package com.cincc.cincc_pbx.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import com.cincc.cincc_pbx.R;
import com.cincc.cincc_pbx.common.MyUtils;
import com.cincc.cincc_pbx.constant.CallConstant;
import com.cincc.cincc_pbx.model.EventBusModel;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class DialFragment extends Fragment implements View.OnClickListener {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dial, container, false);
        initView(view);
        return view;
    }

    private void applyPermission() {
        XXPermissions.with(getActivity()).permission(CallConstant.AUDIO_PERMISSION_LIST)
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                        if (!allGranted) {
                            Toast.makeText(getActivity(), "请授予全部权限，否则无法使用通话功能", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        EventBus.getDefault().post(new EventBusModel(CallConstant.MAKE_CALL, phoneEdit.getText().toString()));
                    }

                    @Override
                    public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                        if (doNotAskAgain) {
                            Toast.makeText(getActivity(), "被永久拒绝授权，请手动授予对应权限", Toast.LENGTH_SHORT).show();
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(getActivity(), permissions);
                        } else {
                            System.out.println("授予权限失败");
                        }
                    }
                });
    }

    EditText phoneEdit;

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.delete_img) {
            String data = phoneEdit.getText().toString();
            if (data.length() > 0) {
                data = data.substring(0, data.length() - 1);
                phoneEdit.setText(data);
            }
        }
        if (v.getId() == R.id.call_img) {
            applyPermission();
        }
    }

    private void initView(View view) {
        RelativeLayout titleText = view.findViewById(R.id.top_relayout);
        int statusBarHeight = ImmersionBar.getStatusBarHeight(this);
        ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) titleText.getLayoutParams();
        lp.topMargin = statusBarHeight + 20;
        titleText.setLayoutParams(lp);

        phoneEdit = view.findViewById(R.id.phone_edit);
        view.findViewById(R.id.delete_img).setOnClickListener(this);
        view.findViewById(R.id.call_img).setOnClickListener(this);
        initDialBtn(view);
    }

    private void initDialBtn(View view) {
        List<RelativeLayout> layoutList = new ArrayList<>();
        layoutList.add(view.findViewById(R.id.layout_dial_one));
        layoutList.add(view.findViewById(R.id.layout_dial_two));
        layoutList.add(view.findViewById(R.id.layout_dial_three));
        layoutList.add(view.findViewById(R.id.layout_dial_four));
        layoutList.add(view.findViewById(R.id.layout_dial_five));
        layoutList.add(view.findViewById(R.id.layout_dial_six));
        layoutList.add(view.findViewById(R.id.layout_dial_seven));
        layoutList.add(view.findViewById(R.id.layout_dial_eight));
        layoutList.add(view.findViewById(R.id.layout_dial_nine));
        layoutList.add(view.findViewById(R.id.layout_dial_star));
        layoutList.add(view.findViewById(R.id.layout_dial_zero));
        layoutList.add(view.findViewById(R.id.layout_dial_jing));

        List<TextView> textList = new ArrayList<>();
        textList.add(view.findViewById(R.id.text_one));
        textList.add(view.findViewById(R.id.text_two));
        textList.add(view.findViewById(R.id.text_three));
        textList.add(view.findViewById(R.id.text_four));
        textList.add(view.findViewById(R.id.text_five));
        textList.add(view.findViewById(R.id.text_six));
        textList.add(view.findViewById(R.id.text_seven));
        textList.add(view.findViewById(R.id.text_eight));
        textList.add(view.findViewById(R.id.text_nine));
        textList.add(view.findViewById(R.id.text_xing));
        textList.add(view.findViewById(R.id.text_zero));
        textList.add(view.findViewById(R.id.text_jing));

        List<TextView> textEngList = new ArrayList<>();
        textEngList.add(view.findViewById(R.id.text_eng_one));
        textEngList.add(view.findViewById(R.id.text_eng_two));
        textEngList.add(view.findViewById(R.id.text_eng_three));
        textEngList.add(view.findViewById(R.id.text_eng_four));
        textEngList.add(view.findViewById(R.id.text_eng_five));
        textEngList.add(view.findViewById(R.id.text_eng_six));
        textEngList.add(view.findViewById(R.id.text_eng_seven));
        textEngList.add(view.findViewById(R.id.text_eng_eight));
        textEngList.add(view.findViewById(R.id.text_eng_nine));
        textEngList.add(view.findViewById(R.id.text_eng_xing));
        textEngList.add(view.findViewById(R.id.text_eng_zero));
        textEngList.add(view.findViewById(R.id.text_eng_jing));


        for (int i = 0; i < layoutList.size(); i++) {
            int finalI = i;
            layoutList.get(i).setOnTouchListener((v, event) -> {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        layoutList.get(finalI).setBackground(getActivity().getDrawable(R.drawable.dial_btn_press_bg));
                        textList.get(finalI).setTextColor(getResources().getColor(R.color.white));
                        textEngList.get(finalI).setTextColor(getResources().getColor(R.color.white));
                        String number = MyUtils.getNumber(finalI);
                        phoneEdit.setText(String.format("%s" + number, phoneEdit.getText().toString()));
                        break;
                    case MotionEvent.ACTION_UP:
                        layoutList.get(finalI).setBackground(getActivity().getDrawable(R.drawable.dial_btn_normal_bg));
                        textList.get(finalI).setTextColor(getResources().getColor(R.color.N2));
                        textEngList.get(finalI).setTextColor(getResources().getColor(R.color.N3));
                        break;
                }
                return true;
            });
        }
    }


}
